package com.magus.project.yddemo.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseEntity;
import com.magus.framework.core.exception.app.ControllerException;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.project.yddemo.entity.IndicatorMapperReport1;
import com.magus.project.yddemo.entity.IndicatorMapperReport2;
//import com.magus.project.yddemo.entity.IndicatorMapperReport2;
import com.magus.project.yddemo.entity.IndicatorMapperReport3;
import com.magus.project.yddemo.entity.IndicatorMapperReport4;
import com.magus.project.yddemo.entity.IndicatorMapperReport5;
import com.magus.project.yddemo.entity.IndicatorMapperReport6;
import com.magus.project.yddemo.service.IndicatorMapperReport1Service;
import com.magus.project.yddemo.service.IndicatorMapperReport2Service;
import com.magus.project.yddemo.service.IndicatorMapperReport3Service;
import com.magus.project.yddemo.service.IndicatorMapperReport4Service;
import com.magus.project.yddemo.service.IndicatorMapperReport5Service;
import com.magus.project.yddemo.service.IndicatorMapperReport6Service;
import com.magus.project.yddemo.webbean.IndicatorMapperReportSaveWebBean;
import com.magus.project.yddemo.webbean.IndicatorReportUpdateWebBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 报表管理
 * 
 * @author wenyu.han
 * @date 2018-03-19 11:47:36
 */
@Api(tags = "IndicatorReportManager", description = "报表管理")
@RestController
@RequestMapping("/yddemo/indicatorReport")
@AppGroupInfo(key = "yddemo:indicatorReport", name = "报表管理", appKey = "yddemo")
public class IndicatorReportController extends BaseController {
	private static final String RESPOST1 = "resport_1";
	private static final String RESPOST2 = "resport_2";
	private static final String RESPOST3 = "resport_3";
	private static final String RESPOST4 = "resport_4";
	private static final String RESPOST5 = "resport_5";
	private static final String RESPOST6 = "resport_6";

	@Autowired
	private IndicatorMapperReport1Service report1Service;

	@Autowired
	private IndicatorMapperReport2Service report2Service;

	@Autowired
	private IndicatorMapperReport3Service report3Service;

	@Autowired
	private IndicatorMapperReport4Service report4Service;

	@Autowired
	private IndicatorMapperReport5Service report5Service;

	@Autowired
	private IndicatorMapperReport6Service report6Service;

	@GetMapping
	@RequiresPermissions("yddemo:indicatorReport:view")
	@ApiOperation(value = "查询报表", notes = "查询报表")
	@AppFunctionInfo(key = "yddemo:indicatorReport:view", name = "发电生产能力报表", groupKey = "yddemo:indicatorReport", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorReport")
	ResponseEntity<List<? extends BaseEntity>> findReport(@RequestParam String reportId, @RequestParam Long time) {
		List<? extends BaseEntity> findByTime = new ArrayList<>();
		//分报表调用查询方法
		if (RESPOST1.equals(reportId)) {
			findByTime = report1Service.findByTime(time);
		} else if (RESPOST2.equals(reportId)) {
			findByTime = report2Service.findByTime(time);
		} else if (RESPOST3.equals(reportId)) {
			findByTime = report3Service.findByTime(time);
		} else if (RESPOST4.equals(reportId)) {
			findByTime = report4Service.findByTime(time);
		} else if (RESPOST5.equals(reportId)) {
			findByTime = report5Service.findByTime(time);
		} else if (RESPOST6.equals(reportId)) {
			findByTime = report6Service.findByTime(time);
		}
		return ResponseEntity.ok(findByTime);
	}

	/**
	 * 保存发电生产能力报表
	 * 
	 * @param report
	 * @param result
	 * @return
	 */
	@PutMapping
	@RequiresPermissions("yddemo:indicatorReport:save")
	@ApiOperation(value = "保存指标授权数据", notes = "保存指标授权数据")
	@AppFunctionInfo(key = "yddemo:indicatorReport:save", name = "保存指标授权数据", groupKey = "yddemo:indicatorReport", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorReport/page/save")
	ResponseEntity<List<? extends BaseEntity>> addReport(@RequestBody IndicatorMapperReportSaveWebBean report,
			BindingResult result) {
		List<? extends BaseEntity> save = new ArrayList<>();
		//验证是否存在空字段
		if (RESPOST1.equals(report.getReportId())) {
			List<IndicatorMapperReport1> report1 = report.getReport1();
			report1 = report1.parallelStream().map(item -> {
				item.setFdl(item.getFdl() == null ? 0.0 : item.getFdl());
				item.setBnjsnl(item.getBnjsnl() == null ? 0.0 : item.getBnjsnl());
				item.setNcscnl(item.getNcscnl() == null ? 0.0 : item.getNcscnl());
				item.setNcscnl_hj(item.getNcscnl_hj() == null ? 0.0 : item.getNcscnl_hj());
				item.setNcscnl_jgxz(item.getNcscnl_jgxz() == null ? 0.0 : item.getNcscnl_jgxz());
				item.setNcscnl_jjxz(item.getNcscnl_jjxz() == null ? 0.0 : item.getNcscnl_jjxz());
				item.setNcscnl_qtxz(item.getNcscnl_qtxz() == null ? 0.0 : item.getNcscnl_qtxz());
				item.setNmscnl(item.getNmscnl() == null ? 0.0 : item.getNmscnl());
				return item;
			}).collect(Collectors.toList());
			validate(report1, result);
			save = report1Service.save(report1);
		} else if (RESPOST2.equals(report.getReportId())) {
			List<IndicatorMapperReport2> report2 = report.getReport2();
			report2 = report2.parallelStream().map(item -> {
				item.setBzmh_fd(item.getBzmh_fd() == null ? 0.0 : item.getBzmh_fd());
				item.setBzmh_gd(item.getBzmh_gd() == null ? 0.0 : item.getBzmh_gd());
				item.setFdcydl(item.getFdcydl() == null ? 0.0 : item.getFdcydl());
				item.setFdcydlv(item.getFdcydlv() == null ? 0.0 : item.getFdcydlv());
				item.setFdhyry(item.getFdhyry() == null ? 0.0 : item.getFdhyry());
				item.setFdhytrq(item.getFdhytrq() == null ? 0.0 : item.getFdhytrq());
				item.setFdhyym(item.getFdhyym() == null ? 0.0 : item.getFdhyym());
				item.setFdl_hj(item.getFdl_hj() == null ? 0.0 : item.getFdl_hj());
				item.setFdl_syx(item.getFdl_syx() == null ? 0.0 : item.getFdl_syx());
				item.setFdxhbzml(item.getFdxhbzml() == null ? 0.0 : item.getFdxhbzml());
				item.setGrbzmh(item.getGrbzmh() == null ? 0.0 : item.getGrbzmh());
				item.setGrbzml(item.getGrbzml() == null ? 0.0 : item.getGrbzml());
				item.setGrcyd_cdylv(item.getGrcyd_cdylv() == null ? 0.0 : item.getGrcyd_cdylv());
				item.setGrcyd_cydl(item.getGrcyd_cydl() == null ? 0.0 : item.getGrcyd_cydl());
				item.setGrl(item.getGrl() == null ? 0.0 : item.getGrl());
				item.setGrrlxh_ry(item.getGrrlxh_ry() == null ? 0.0 : item.getGrrlxh_ry());
				item.setGrrlxh_trq(item.getGrrlxh_trq() == null ? 0.0 : item.getGrrlxh_trq());
				item.setGrrlxh_ym(item.getGrrlxh_ym() == null ? 0.0 : item.getGrrlxh_ym());
				item.setGrsbrl(item.getGrsbrl() == null ? 0.0 : item.getGrsbrl());
				item.setJscydldfdl(item.getJscydldfdl() == null ? 0.0 : item.getJscydldfdl());
				item.setJsfdmhdfdl(item.getJsfdmhdfdl() == null ? 0.0 : item.getJsfdmhdfdl());
				item.setJsgdmhdgdl(item.getJsgdmhdgdl() == null ? 0.0 : item.getJsgdmhdgdl());
				item.setPjlyxs(item.getPjlyxs() == null ? 0.0 : item.getPjlyxs());
				item.setPjsbrl(item.getPjsbrl() == null ? 0.0 : item.getPjsbrl());
				item.setQmfdsbrl(item.getQmfdsbrl() == null ? 0.0 : item.getQmfdsbrl());
				return item;
			}).collect(Collectors.toList());
			validate(report2, result);
			save = report2Service.save(report2);
		} else if (RESPOST3.equals(report.getReportId())) {
			List<IndicatorMapperReport3> report3 = report.getReport3();
			report3 = report3.parallelStream().map(item -> {
				item.setFdl_by_hj(item.getFdl_by_hj() == null ? 0.0 : item.getFdl_by_hj());
				item.setFdl_by_syx(item.getFdl_by_syx() == null ? 0.0 : item.getFdl_by_syx());
				item.setFdl_lj_hj(item.getFdl_lj_hj() == null ? 0.0 : item.getFdl_lj_hj());
				item.setFdl_lj_syx(item.getFdl_lj_syx() == null ? 0.0 : item.getFdl_lj_syx());
				item.setGrsbrl(item.getGrsbrl() == null ? 0.0 : item.getGrsbrl());
				item.setPjlyxs_by(item.getPjlyxs_by() == null ? 0.0 : item.getPjlyxs_by());
				return item;
			}).collect(Collectors.toList());
			validate(report3, result);
			save = report3Service.save(report3);
		} else if (RESPOST4.equals(report.getReportId())) {
			List<IndicatorMapperReport4> report4 = report.getReport4();
			report4 = report4.parallelStream().map(item -> {
				item.setCydl_by(item.getCydl_by() == null ? 0.0 : item.getCydl_by());
				item.setCydl_lj(item.getCydl_lj() == null ? 0.0 : item.getCydl_lj());
				item.setFdl_by(item.getFdl_by() == null ? 0.0 : item.getFdl_by());
				item.setFdl_lj(item.getFdl_lj() == null ? 0.0 : item.getFdl_lj());
				item.setZjcydl_by(item.getZjcydl_by() == null ? 0.0 : item.getZjcydl_by());
				item.setZjcydl_lj(item.getZjcydl_lj() == null ? 0.0 : item.getZjcydl_lj());
				item.setZjcydlv_by(item.getZjcydlv_by() == null ? 0.0 : item.getZjcydlv_by());
				item.setZjcydlv_lj(item.getZjcydlv_lj() == null ? 0.0 : item.getZjcydlv_lj());
				return item;
			}).collect(Collectors.toList());
			validate(report4, result);
			save = report4Service.save(report4);
		} else if (RESPOST5.equals(report.getReportId())) {
			List<IndicatorMapperReport5> report5 = report.getReport5();
			report5 = report5.parallelStream().map(item -> {
				item.setBzmh_fd(item.getBzmh_fd()==null?0.0:item.getBzmh_fd());
				item.setBzmh_gd(item.getBzmh_gd()==null?0.0:item.getBzmh_gd());
				item.setBzml_hj(item.getBzml_hj()==null?0.0:item.getBzml_hj());
				item.setBzml_mz(item.getBzml_mz()==null?0.0:item.getBzml_mz());
				item.setBzml_qt(item.getBzml_qt()==null?0.0:item.getBzml_qt());
				item.setBzml_trqz(item.getBzml_trqz()==null?0.0:item.getBzml_trqz());
				item.setBzml_yz(item.getBzml_yz()==null?0.0:item.getBzml_yz());
				item.setFdl(item.getFdl()==null?0.0:item.getFdl());
				item.setGdl(item.getGdl()==null?0.0:item.getGdl());
				item.setHyry(item.getHyry()==null?0.0:item.getHyry());
				item.setHytrq(item.getHytrq()==null?0.0:item.getHytrq());
				item.setHyym(item.getHyym()==null?0.0:item.getHyym());
				return item;
			}).collect(Collectors.toList());
			validate(report5, result);
			save = report5Service.save(report5);
		} else if (RESPOST6.equals(report.getReportId())) {
			 List<IndicatorMapperReport6> report6 = report.getReport6();
			 report6=report6.parallelStream().map(item -> {
				 item.setGrbzmh(item.getGrbzmh()==null?0.0:item.getGrbzmh());
				 item.setGrbzml_hj(item.getGrbzml_hj()==null?0.0:item.getGrbzml_hj());
				 item.setGrbzml_mz(item.getGrbzml_mz()==null?0.0:item.getGrbzml_mz());
				 item.setGrbzml_trqz(item.getGrbzml_trqz()==null?0.0:item.getGrbzml_trqz());
				 item.setGrbzml_yz(item.getGrbzml_yz()==null?0.0:item.getGrbzml_yz());
				 item.setGrcyd_cydl(item.getGrcyd_cydl()==null?0.0:item.getGrcyd_cydl());
				 item.setGrcyd_cydlv(item.getGrcyd_cydlv()==null?0.0:item.getGrcyd_cydlv());
				 item.setGrl(item.getGrl()==null?0.0:item.getGrl());
				 item.setGrrlxh_ry(item.getGrrlxh_ry()==null?0.0:item.getGrrlxh_ry());
				 item.setGrrlxh_trq(item.getGrrlxh_trq()==null?0.0:item.getGrrlxh_trq());
				 item.setGrrlxh_ym(item.getGrrlxh_ym()==null?0.0:item.getGrrlxh_ym());
				 item.setQmgrsbrl(item.getQmgrsbrl()==null?0.0:item.getQmgrsbrl());
				 
			 return item;
			 }).collect(Collectors.toList());
			 validate(report6, result);
			 save = report6Service.save(report6);
		}
		return ResponseEntity.status(HttpStatus.CREATED).body(save);
	}

	@PostMapping
	@RequiresPermissions("yddemo:indicatorRepoet:update")
	@ApiOperation(value = "更新报表数据", notes = "更新报表数据")
	@AppFunctionInfo(key = "yddemo:indicatorRepoet:update", name = "更新报表数据", groupKey = "yddemo:indicatorReport", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorReport")
	ResponseEntity<Boolean> updataReport1(@RequestBody IndicatorReportUpdateWebBean webBean, BindingResult result) {
		Boolean updateDatas = false;
		//分报表调用更新方法
		if (RESPOST1.equals(webBean.getReportId())) {
			updateDatas = report1Service.updateDatas(webBean.getTime());
		} else if (RESPOST2.equals(webBean.getReportId())) {
			updateDatas = report2Service.updateDatas(webBean.getTime());
		} else if (RESPOST3.equals(webBean.getReportId())) {
			updateDatas = report3Service.updateDatas(webBean.getTime());
		} else if (RESPOST4.equals(webBean.getReportId())) {
			updateDatas = report4Service.updateDatas(webBean.getTime());
		} else if (RESPOST5.equals(webBean.getReportId())) {
			updateDatas = report5Service.updateDatas(webBean.getTime());
		} else if (RESPOST6.equals(webBean.getReportId())) {
			updateDatas = report6Service.updateDatas(webBean.getTime());
		}
		return ResponseEntity.status(HttpStatus.CREATED).body(updateDatas);
	}

	@GetMapping("/export")
	ResponseEntity<Void> exportReport(@RequestParam Long time, @RequestParam String reportId,
			HttpServletRequest request, HttpServletResponse response) {
		// 设置response返回的数据格式，防止在IE中请求成功后 提示下载信息
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.TEXT_HTML);
		HSSFWorkbook workBook = null;
		String reportName = "";
		switch (reportId) {
		case RESPOST1:
			// 查询报表数据
			List<IndicatorMapperReport1> report1 = report1Service.findByTime(time);
			// 生成报表
			workBook = report1Service.exportReport(report1);
			reportName = "resport_1";
			break;
		case RESPOST2:
			// 查询报表数据
			List<IndicatorMapperReport2> report2 = report2Service.findByTime(time);
			// 生成报表
			workBook = report2Service.exportReport(report2);
			reportName = "resport_2";
			break;
		case RESPOST3:
			// 查询报表数据
			List<IndicatorMapperReport3> report3 = report3Service.findByTime(time);
			// 生成报表
			workBook = report3Service.exportReport(report3);
			reportName = "resport_3";
			break;
		case RESPOST4:
			// 查询报表数据
			List<IndicatorMapperReport4> report4 = report4Service.findByTime(time);
			// 生成报表
			workBook = report4Service.exportReport(report4);
			reportName = "resport_4";
			break;
		case RESPOST5:
			// 查询报表数据
			List<IndicatorMapperReport5> report5 = report5Service.findByTime(time);
			// 生成报表
			workBook = report5Service.exportReport(report5);
			reportName = "resport_5";
			break;
		case RESPOST6:
			// 查询报表数据
			List<IndicatorMapperReport6> report6 = report6Service.findByTime(time);
			// 生成报表
			workBook = report6Service.exportReport(report6);
			reportName = "resport_6";
			break;
		}
		// 清空输出流
		response.reset();
		// 设置返回的格式
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		// 设置返回的名称
		response.setHeader("Content-Disposition", "attachment;filename=" + reportName + ".xls");
		try {
			// 输入workbook的流
			OutputStream stream = response.getOutputStream();
			workBook.write(stream);
		} catch (IOException e) {
			throw new ControllerException("APP:Report:Export", "IO错误。" + e.getMessage());
		}
		return null;
	}
}
